import ajax from "./requestApi";
class jsAuthorityClass {
  constructor(props) {
    this.user = null;

    var strJson = sessionStorage.getItem("user");
    if (strJson == null || strJson == "undefined") {
      return;
    }
    var user = JSON.parse(strJson);
    this.user = user;
  }
  loadAuthority(succ) {
    //加载权限信息 ajax
    var _this = this;
    if (_this.user == null) {
      try{
      ajax({
        url: "/newapp/merchantApp/index/queryUserData.do",
        type: "get",
        controlLoading: false
      })
        .then(function(result) {
          if (result && result.success && String(result.success) == "true") {
            var user = result.resultObject;
            sessionStorage.setItem("user", JSON.stringify(user));
            _this.user = user;
            succ && succ();
          } else {

          }
        })
        .catch(function(err) {

        });
      }catch(e){

      }
      return true;
    }

    return false;
  }
  checkedList(list, code) {
    var checkedFlag = false;
    if (list) {
      for (var i = 0; i < list.length; i++) {
        var item = list[i];
        if (item && item.taskCode == code) {
          checkedFlag = true;
          break;
        }
        if (item.children && item.children.length > 0) {
          checkedFlag = this.checkedList(item.children, code);
        }
        if (checkedFlag) {
          break;
        }
      }
    }
    return checkedFlag;
  }
  /**
   * 验证是否有权限操作
   * @param {} code
   */
  checked(code) {
    var checkedFlag = false;
    var user = this.user;
    if (user && user.authMinus) {
      checkedFlag = this.checkedList(user.authMinus, code);
    }
    return checkedFlag;
  }
  checkedSuccess(succ) {
    var flag = this.loadAuthority(succ);
    if (!flag) {
      succ && succ();
    }
  }
}
export default new jsAuthorityClass();
